package de.tum.beilmaier.im.client.bundle;

import java.util.LinkedList;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.IsSerializable;
import com.google.gwt.user.client.ui.TabPanel;

import de.novanic.eventservice.client.event.Event;
import de.novanic.eventservice.client.event.RemoteEventService;
import de.novanic.eventservice.client.event.RemoteEventServiceFactory;
import de.novanic.eventservice.client.event.domain.Domain;
import de.novanic.eventservice.client.event.listener.RemoteEventListener;
import de.tum.beilmaier.im.client.InstantChat;
import de.tum.beilmaier.im.client.MessagingService;
import de.tum.beilmaier.im.client.MessagingServiceAsync;
import de.tum.beilmaier.im.client.UserSessionHandler;
import de.tum.beilmaier.im.client.controller.ChatRoomController;
import de.tum.beilmaier.im.client.event.CloseChatRoomEvent;
import de.tum.beilmaier.im.client.event.ContactListUpdateEvent;
import de.tum.beilmaier.im.client.event.CreateChatRoomEvent;
import de.tum.beilmaier.im.client.event.IncomingMessageEvent;
import de.tum.beilmaier.im.client.view.ChatRoomWindow;
import de.tum.beilmaier.im.shared.model.ChatRoom;
import de.tum.beilmaier.im.shared.model.InstantMessage;
import de.tum.beilmaier.im.shared.model.UUID;
import de.tum.beilmaier.im.shared.model.User;

public class ChatRoomBundle implements RemoteEventListener {

	private Domain CHATROOM_UUID = null;
	private ChatRoom chatRoom = null;
	private ChatRoomWindow chatRoomWindow = null;
	private ChatRoomController chatRoomController = null;
	private final RemoteEventServiceFactory theEventServiceFactory = RemoteEventServiceFactory.getInstance();
	private final RemoteEventService theEventService = theEventServiceFactory.getRemoteEventService();
	private LinkedList<InstantMessage> roomMessages = null;
	private UserSessionHandler userhandler = null;
	private int tabPanelIndex =-1;
	private InstantChat imChat =null;
	
	public ChatRoomBundle(ChatRoom chatRoom, UserSessionHandler userhandler, InstantChat imChat)
	{
	this.CHATROOM_UUID = chatRoom.getUUID();
	this.chatRoom = chatRoom;
	this.userhandler = userhandler;
	this.imChat = imChat;
	
	chatRoomWindow = new ChatRoomWindow(chatRoom);
	chatRoomController = new ChatRoomController(chatRoomWindow, userhandler, imChat);
	roomMessages = new LinkedList<InstantMessage>();
	
	theEventService.addListener(chatRoom.getUUID(), this);
	System.out.println("Registering Listener for ChatRoom.." + chatRoom.getUUID().toString());
	
	}
	
	public ChatRoomBundle(){}

	public Domain getCHATROOM_UUID() {
		return CHATROOM_UUID;
	}

	public void setCHATROOM_UUID(Domain cHATROOM_UUID) {
		CHATROOM_UUID = cHATROOM_UUID;
	}

	public ChatRoom getChatRoom() {
		return chatRoom;
	}

	public void setChatRoom(ChatRoom chatRoom) {
		this.chatRoom = chatRoom;
	}

	public ChatRoomWindow getChatRoomWindow() {
		return chatRoomWindow;
	}

	public void setChatRoomWindow(ChatRoomWindow chatRoomWindow) {
		this.chatRoomWindow = chatRoomWindow;
	}

	public ChatRoomController getChatRoomController() {
		return chatRoomController;
	}

	public void setChatRoomController(ChatRoomController chatRoomController) {
		this.chatRoomController = chatRoomController;
	}

	@Override
	public void apply(Event anEvent) {
		System.out.println("event for chatRoom Triggered..!");
		if (anEvent instanceof IncomingMessageEvent)
			onMyEvent((IncomingMessageEvent) anEvent);
		
		if (anEvent instanceof CloseChatRoomEvent)
			onMyEvent((CloseChatRoomEvent) anEvent);
		
		
	}

	private void onMyEvent(CloseChatRoomEvent anEvent) {
	
	ChatRoom toclose = anEvent.getChatRoom();
	
	if (chatRoom.getUUID().equals(toclose.getUUID()))
	{
		System.out.println("Remove Bundle and Close ChatRoomTab... "+chatRoom.getRoomId() + "try to remove at Index" + tabPanelIndex+" for User"+userhandler.getLoginUser().getVorname());
		
		if (tabPanelIndex >= 0)
		imChat.getTabPanel().remove(tabPanelIndex);
		userhandler.getLoginUser().removeChatRoom(toclose);
		userhandler.getUserChatRoomBundles().remove(toclose.getUUID());
		tabPanelIndex = -1;
		
	}
		
	}

	private void onMyEvent(IncomingMessageEvent anEvent) {
		System.out.println("CatRoomBundle - received Message" + anEvent.getInstantMessage().getMessage());
		roomMessages.add(anEvent.getInstantMessage());
		String old_text = chatRoomWindow.getReceiveArea().getText();
		String new_text = old_text+ anEvent.getInstantMessage().getSendUser().getName()+":" + anEvent.getInstantMessage().getMessage()+ "\n";
		chatRoomWindow.getReceiveArea().setText(new_text);
	}

	public void setTabPanelIndex(int i) {
		tabPanelIndex = i;
		
	}
	
	public int getTabPanelIndex() {
		return tabPanelIndex;
		
	}
	
}
